﻿using UnityEditor;
using UnityEngine;

namespace XFGameFramework
{

    [CustomEditor(typeof(AudioPlayer))]
    public class AudioPlayerInspector : Editor
    {
        private SerializedObject audioPlayerSerialized;

        SerializedProperty audioSource;
        SerializedProperty controllerName;
        SerializedProperty audioAssetType;
        SerializedProperty projectName;
        SerializedProperty assetName;
        SerializedProperty clip;
        SerializedProperty audioPlayType;
        SerializedProperty audioPlayPosition;
        SerializedProperty playPosition;
        SerializedProperty playOnAwake;

        
        private void OnEnable()
        {

            audioPlayerSerialized = new SerializedObject(target);

            audioSource = audioPlayerSerialized.FindProperty("audioSource");
            controllerName = audioPlayerSerialized.FindProperty("controllerName");
            audioAssetType = audioPlayerSerialized.FindProperty("audioAssetType");
            projectName = audioPlayerSerialized.FindProperty("projectName");
            assetName = audioPlayerSerialized.FindProperty("assetName");
            clip = audioPlayerSerialized.FindProperty("clip");
            audioPlayType = audioPlayerSerialized.FindProperty("audioPlayType");
            audioPlayPosition = audioPlayerSerialized.FindProperty("audioPlayPosition");
            playPosition = audioPlayerSerialized.FindProperty("playPosition");
            playOnAwake = audioPlayerSerialized.FindProperty("playOnAwake");
        }

        public override void OnInspectorGUI()
        {
            //base.OnInspectorGUI();
            audioPlayerSerialized.Update();
            AudioPlayer obj = target as AudioPlayer;

            if (obj != null)
            {
                AudioSource source = obj.GetComponent<AudioSource>();
                if (source != null)
                {
                    if (audioSource.objectReferenceValue != source) 
                        audioSource.objectReferenceValue = source;

                    

                    EditorGUI.BeginDisabledGroup(true); 
                    EditorGUILayout.PropertyField(audioSource); 
                    EditorGUI.EndDisabledGroup();
                }
            }
            else {
                Debug.Log("not gameobject");
            }
            
            EditorGUILayout.PropertyField(controllerName);
            EditorGUILayout.PropertyField(audioAssetType);

            switch (audioAssetType.enumValueIndex)
            {
                case (int)AudioAssetType.AudioClip: 
                    EditorGUILayout.PropertyField(clip);
                    break;
                case (int)AudioAssetType.AssetBundle:
                    EditorGUILayout.PropertyField(projectName);
                    EditorGUILayout.PropertyField(assetName);
                    break;
            }


            EditorGUILayout.PropertyField(audioPlayType);
            EditorGUILayout.PropertyField(audioPlayPosition);

            if (audioPlayPosition.enumValueIndex == (int)AudioPlayPosition.CustomPosition)
                EditorGUILayout.PropertyField(playPosition);

            EditorGUILayout.PropertyField(playOnAwake);
             
            audioPlayerSerialized.ApplyModifiedProperties();
        }

    }
}

